<?php
namespace app\mapi\controller;
use app\mapi\model\MallShopsModel;
use app\mapi\model\MallGoodsModel;
use app\mapi\model\MallOrderModel;

/**
 * 订单控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class Order extends Base{

    # 结算订单
    public function mallSettlement(){
        if(request()->isPost()){
            $Shops = input('post.shops','','trim');

            $Shops = explode(',', $Shops);

            $Msgs = [];

            for($i = 0; $i < count($Shops); $i++){ 
                array_push($Msgs, $Shops[$i]);
            }

            $this->ShowAjax(1, $Msgs);
        }
    }

    # 支付订单
    public function orderPayment(){
        if(request()->isPost()){
            $this->ShowAjax(1, '待开发');
        }
    }

    # 确认收货
    public function orderReceiving(){
        if(request()->isPost()){
            $Order = input('post.order_id','','intval');
            $Token = input('post.token','','trim');

            if(empty($Order)){
                $this->ShowAjax(2, '缺少order_id参数');
            }

            $FindUser = $this->checkLogin($Token);

            $Where = [];
            $Where['order_id'] = $Order;
            $Where['order_user'] = $FindUser['user_id'];
            $Where['order_state'] = 3;

            $MallOrder = new MallOrderModel;

            $FindOrder = $MallOrder->mInfo($Where);

            if(!empty($FindOrder)){
                $Param = [];
                $Param['order_state'] = 4;

                $BackOrder = $MallOrder->mEdit($Where, $Param);

                $Code = $BackOrder['code'];
                if($Code == 1){
                    $Msgs = '确认收货成功';
                }else{
                    $Msgs = '确认收货失败';
                }
            }else{
                $Code = 2;
                $Msgs = '查无订单';
            }

            $this->ShowAjax($Code, $Msgs);
        }
    }

    # 取消订单
    public function orderCancel(){
        if(request()->isPost()){
            $Order = input('post.order_id','','intval');
            $Token = input('post.token','','trim');

            if(empty($Order)){
                $this->ShowAjax(2, '缺少order_id参数');
            }

            $FindUser = $this->checkLogin($Token);

            $Where = [];
            $Where['order_id'] = $Order;
            $Where['order_user'] = $FindUser['user_id'];
            $Where['order_state'] = 1;

            $MallOrder = new MallOrderModel;

            $FindOrder = $MallOrder->mInfo($Where);

            if(!empty($FindOrder)){
                $Param = [];
                $Param['order_state'] = 8;

                $BackOrder = $MallOrder->mEdit($Where, $Param);
            }else{
                $Code = 2;
                $Msgs = '查无订单';
            }

            $this->ShowAjax($Code, $Msgs);
        }
    }

}